package com.android.androidbigwork;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.PopupMenu;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.android.androidbigwork.adapter.Demo_List_Adapter;
import com.android.androidbigwork.entity.RequestParas;
import com.android.androidbigwork.frames.Demo;
import com.android.androidbigwork.frames.My;
import com.android.androidbigwork.frames.Shares;
import com.android.androidbigwork.utils.MyCallBack;
import com.android.androidbigwork.utils.RequestUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;


public class MainActivity extends AppCompatActivity {

    private  BottomNavigationView bottomNavigationView ;
    private Demo demoFragment =null;
    private My myFragment = null;
    private Shares sharesFragment =null;
    public static final String POSITION = "position";
    private int position;
    FragmentManager fragmentManager;
    private static final String getSharesurl =
            "http://39.108.183.17:8080/getShare";
    private static final String getMySharesurl =
            "http://39.108.183.17:8080/getmyShare";

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity_layout);


        bottomNavigationView = findViewById(R.id.bottom_navigation);

        fragmentManager = getSupportFragmentManager();

        showFragment(R.id.navi_demo);


        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                if (menuItem.getItemId() == R.id.navi_demo) {
                    Log.d("" ,"onNavigationItemSelected: 点击了首页");

                    Demo demo = (Demo) fragmentManager.getFragments().get(0);
                    if(demo!=null) {

                        ListView listView = demo.getActivity().findViewById(R.id.demoList);
                        if (listView != null) {
                            Demo_List_Adapter demo_list_adapter = (Demo_List_Adapter) listView.getAdapter();
                            if (demo_list_adapter != null) {
                                MyCallBack myCallBack = new MyCallBack(demo);
                                myCallBack.setFragment(demo);
                                myCallBack.setDemo_list_adapter(demo_list_adapter);
                                RequestUtils.flag = true;
                                RequestUtils.getShares(getSharesurl, myCallBack);
//                                demo_list_adapter.notifyDataSetChanged();

                            }
                        }
                    }
                }
                else if(menuItem.getItemId()==R.id.my){
//                    Log.d("" ,"onNavigationItemSelected: 点击了我的");

                    List<Fragment> fragments = fragmentManager.getFragments();
                    My demo = null;
                    for(Fragment fragment:fragments){
                        if(fragment instanceof My){
                            demo = (My) fragment;
                        }
                    }
                    if(demo!=null) {
//                        Log.d("", "onNavigationItemSelected:demo!=null ");
                        ListView listView = demo.getActivity().findViewById(R.id.demoList);
                        if (listView != null) {
                            Demo_List_Adapter demo_list_adapter = (Demo_List_Adapter) listView.getAdapter();
                            if (demo_list_adapter != null) {
//                                Log.d("TAG", "onNavigationItemSelected: 我的页面的适配器不为空");
                                MyCallBack myCallBack = new MyCallBack(demo);
                                myCallBack.setFragment(demo);
                                myCallBack.setFlag(false);
                                myCallBack.setDemo_list_adapter(demo_list_adapter);
                                RequestUtils.flag =false;
                                RequestUtils.getShares(getMySharesurl, myCallBack);

                            }
                        }
                    }
                }

                showFragment(menuItem.getItemId());
                return true;
            }
        });
    }



    private void showFragment(int anInt) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);

        position = anInt;

        switch (anInt){
            case R.id.navi_demo:
                if(demoFragment==null){
                    demoFragment = new Demo();
                    transaction.add(R.id.frameLayout,demoFragment);
                }else {
                    transaction.show(demoFragment);
                }
                break;
            case R.id.navi_share:
                if(sharesFragment==null){
                    sharesFragment = new Shares();
                    transaction.add(R.id.frameLayout,sharesFragment);
                }else {
                    transaction.show(sharesFragment);
                }
                break;
            case R.id.my:
                if (myFragment==null){
                    myFragment = new My();
                    transaction.add(R.id.frameLayout,myFragment);
                }else
                {
                    transaction.show(myFragment);
                }
                break;
        }

        transaction.commit();
    }


    private void hideFragment(FragmentTransaction transaction) {
            if(demoFragment!=null)
                transaction.hide(demoFragment);
            if(myFragment!=null){
                transaction.hide(myFragment);
            }
            if(sharesFragment!=null)
                transaction.hide(sharesFragment);
    }
}
